unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons, ExtCtrls;

const
  N = 9;
  R = 20;
  D = 4;

type
  TForm1 = class(TForm)
    Image1: TImage;
    BitBtn1: TBitBtn;
    procedure BitBtn1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  k, i, j, l, x, y, x0, y0, m: Integer;

implementation

{$R *.dfm}

procedure TForm1.BitBtn1Click(Sender: TObject);
begin
  if Round (ln(N)/ln(2)) >= ln(N)/ln(2) then
  begin
    k:= Round (ln(N)/ln(2));
  end else begin
    k:= Round (ln(N)/ln(2)) + 1;
  end;
  l:= N - (((1+k)*k) div 2);
  m:=k;
  x0:= 200 + k*(R+D);
  x:=x0;
  y0:= k*(R+D) + 100;
  y:=y0;
  for i:= 1 to k do
  begin
    for j:= 1 to m do
    begin
      x:=x-(2*R+D);
      Image1.Canvas.Ellipse(x+R, y+R, x-R, y-R);
    end;
    Dec(m);
    x:= x0 - R*i - (D div 2)*i;
    y:= y - 2*R;
  end;
  for i:= 1 to l do
  begin
    x:= x0 - (2*R+D)*(k-l+i) - R;
    y:= y0 + 2*R;
    Image1.Canvas.Ellipse(x+R, y+R, x-R, y-R);
  end;
end;

end.

